<?php
namespace app\command;

use app\model\AdminModel;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class CreateAdminCommand extends Command
{
    protected static $defaultName = 'admin:create';
    protected static $defaultDescription = '创建管理员账户';

    protected function configure()
    {
        $this->setName('admin:create')
            ->setDescription('创建管理员账户')
            ->addOption('username', null, InputOption::VALUE_OPTIONAL, '用户名', 'admin')
            ->addOption('password', null, InputOption::VALUE_OPTIONAL, '密码', '123456')
            ->addOption('email', null, InputOption::VALUE_OPTIONAL, '邮箱', 'admin@example.com');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try {
            $username = $input->getOption('username');
            $password = $input->getOption('password');
            $email = $input->getOption('email');

            // 检查用户名是否已存在
            if (AdminModel::where('username', $username)->exists()) {
                $output->writeln("<error>用户名 {$username} 已存在</error>");
                return self::FAILURE;
            }

            // 创建管理员
            AdminModel::create([
                'username' => $username,
                'password' => password_hash($password, PASSWORD_DEFAULT),
                'email' => $email,
                'status' => 1
            ]);

            $output->writeln("<info>管理员账户创建成功</info>");
            $output->writeln("用户名: {$username}");
            $output->writeln("密码: {$password}");
            $output->writeln("邮箱: {$email}");

            return self::SUCCESS;
        } catch (\Exception $e) {
            $output->writeln("<error>创建失败: {$e->getMessage()}</error>");
            return self::FAILURE;
        }
    }
} 